﻿using System;
using GalaSoft.MvvmLight;
using System.ComponentModel;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using JmShared;
using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.User;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Views;

namespace JiveMessenger.ViewModels
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class ViewModelAddGroup : ViewModelBaseEx
    {
        private string _nextButtonText;
        private INavigationService _navigationService;
        private IClient _currentClient;
        private bool _isBusy;
        public RelayCommand AddGroupCommand { get; set; }

        private bool _success;
        public bool Success
        {
            get { return _success; }
            set
            {
                if (_success != value)
                {
                    Set(() => Success, ref _success, value);
                }
            }
        }

        private string _inputText;
        public string InputText
        {
            get { return _inputText; }
            set
            {
                if (_inputText != value)
                {
                    Set(() => InputText, ref _inputText, value.Trim());
                    this.AddGroupCommand.RaiseCanExecuteChanged();
                }
            }
        }

        private string _statusText;
        public string StatusText
        {
            get { return _statusText; }
            set
            {
                if (_statusText != value)
                {
                    Set(() => StatusText, ref _statusText, value);
                }
            }
        }

        public string NextButtonText
        {
            get { return _nextButtonText; }
            set
            {
                if (_nextButtonText != value)
                {
                    Set(() => NextButtonText, ref _nextButtonText, value);
                }
            }
        }

        private bool _isInputTextboxEnabled;
        public bool IsInputTextboxEnabled
        {
            get { return _isInputTextboxEnabled; }
            set
            {
                if (_isInputTextboxEnabled != value)
                {
                    Set(() => IsInputTextboxEnabled, ref _isInputTextboxEnabled, value);
                }
            }
        }

        private bool _isNextButtonEnabled;
        public bool IsNextButtonEnabled
        {
            get { return _isNextButtonEnabled; }
            set
            {
                if (_isNextButtonEnabled != value)
                {
                    Set(() => IsNextButtonEnabled, ref _isNextButtonEnabled, value);
                }
            }
        }

        private bool _isCancelButtonEnabled;
        public bool IsCancelButtonEnabled
        {
            get { return _isCancelButtonEnabled; }
            set
            {
                if (_isCancelButtonEnabled != value)
                {
                    Set(() => IsCancelButtonEnabled, ref _isCancelButtonEnabled, value);
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the ViewModelAddGroup class.
        /// </summary>
        public ViewModelAddGroup(IClient currentClient,INavigationService navService)
        {
            _currentClient = currentClient;
            _navigationService = navService;
            IsCancelButtonEnabled = true;
            NextButtonText = "Next";
            //page = 1;

            IsInputTextboxEnabled = true;
            IsNextButtonEnabled = true;
            if (IsInDesignMode)
            {
                StatusText = "this is the status text of your add group query!";
                return;
            }
            AddGroupCommand = new RelayCommand(AddGroupCommandAction, () => !String.IsNullOrEmpty(InputText) && !_isBusy);
        }
        public override void Cleanup()
        {
            SimpleIoc.Default.Unregister<ViewModelAddGroup>(VmHelper.Locator.ViewModelAddGroupKey);
            VmHelper.Locator.ViewModelAddGroupKey = System.Guid.NewGuid().ToString();

            base.Cleanup();
        }
        private async void AddGroupCommandAction()
        {
            if (this.Success)
            {
                _navigationService.GoBack();
                return;
            }
            _isBusy = true;
            this.AddGroupCommand.RaiseCanExecuteChanged();
            ChatMessage msg = new ChatMessage(_currentClient.CurrentUserInfo.ID, _currentClient.CurrentUserInfo.UserName, MessageType.CreateGroup, this.InputText);

            IsNextButtonEnabled = false;
            IsInputTextboxEnabled = false;
            StatusText = string.Empty;
            DatabaseQueryResponse response = await _currentClient.Connection.SendQuery(msg);

            if (response.Success == true)
            {
                Success = true;
                this.StatusText = string.Format("Group {0} created!", this.StatusText);
                NextButtonText = "Done";
                IsCancelButtonEnabled = false;
            }
            else
            {
                Success = false;
                this.StatusText = response.StatusText;
                IsInputTextboxEnabled = true;
            }
            _isBusy = false;
            IsNextButtonEnabled = true;
            this.AddGroupCommand.RaiseCanExecuteChanged();
        }
    }
}